package remote

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/ChanSyncObj"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	mqtt "github.com/eclipse/paho.mqtt.golang"
	"go-fox-edge-iot-thingspanel/component"
	"strings"
)

type mqttHandler struct {
}

func (e *mqttHandler) GetTopic() string {
	return component.LocalConfig.Subscribe
}

func (e *mqttHandler) MqttHandler() mqtt.MessageHandler {
	return func(client mqtt.Client, msg mqtt.Message) {
		if strings.HasSuffix(strings.ToLower(msg.Topic()), "/timestamp") {
			e.handleRespond(string(msg.Payload()))
		}
	}
}

func (e *mqttHandler) handleRespond(jsn string) {
	data := Json.BuildMapByJson(jsn)
	uuid := Map.GetString(data, "uuid", "")
	if Map.GetString(data, "uuid", "") == "" {
		return
	}

	// 将数据转移到队列中，通知发送者查询
	ChanSyncObj.Notify(uuid, data)
}
